package com.yunti.business.service.impl;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.yunti.business.domain.BaseUnit;
import com.yunti.business.mapper.BaseUnitMapper;
import com.yunti.business.utils.IdentityUtils;
import com.yunti.business.vo.webVo.BaseTeacherVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.yunti.business.mapper.BaseTeacherMapper;
import com.yunti.business.domain.BaseTeacher;
import com.yunti.business.service.IBaseTeacherService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.web.multipart.MultipartFile;

/**
 * 教师Service业务层处理
 *
 * @author yangtt
 * @date 2024-11-22
 */
@Service
@Slf4j
public class BaseTeacherServiceImpl extends ServiceImpl<BaseTeacherMapper, BaseTeacher> implements IBaseTeacherService {
    @Autowired
    private BaseTeacherMapper baseTeacherMapper;
    @Autowired
    private BaseUnitMapper unitMapper;

    /**
     * 查询教师
     *
     * @param id 教师主键
     * @return 教师
     */
    @Override
    public BaseTeacher selectBaseTeacherById(Long id) {
        return baseTeacherMapper.selectBaseTeacherById(id);
    }

    /**
     * 查询教师列表
     *
     * @param baseTeacher 教师
     * @return 教师
     */
    @Override
    public List<BaseTeacher> selectBaseTeacherList(BaseTeacher baseTeacher) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        baseTeacher.setRaceId(loginUser.getSelectRaceId());
        return baseTeacherMapper.selectBaseTeacherList(baseTeacher);
    }

    /**
     * 新增教师
     *
     * @param baseTeacher 教师
     * @return 结果
     */
    @Override
    public int insertBaseTeacher(BaseTeacher baseTeacher) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        baseTeacher.setRaceId(loginUser.getSelectRaceId());
        baseTeacher.setCreateTime(LocalDateTime.now());
        return baseTeacherMapper.insertBaseTeacher(baseTeacher);
    }

    /**
     * 修改教师
     *
     * @param baseTeacher 教师
     * @return 结果
     */
    @Override
    public int updateBaseTeacher(BaseTeacher baseTeacher) {
        baseTeacher.setUpdateTime(LocalDateTime.now());
        return baseTeacherMapper.updateBaseTeacher(baseTeacher);
    }

    /**
     * 批量删除教师
     *
     * @param ids 需要删除的教师主键
     * @return 结果
     */
    @Override
    public int deleteBaseTeacherByIds(Long[] ids) {
        return baseTeacherMapper.deleteBaseTeacherByIds(ids);
    }

    /**
     * 删除教师信息
     *
     * @param id 教师主键
     * @return 结果
     */
    @Override
    public int deleteBaseTeacherById(Long id) {
        return baseTeacherMapper.deleteBaseTeacherById(id);
    }

    @Override
    public AjaxResult importData(MultipartFile file, boolean updateSupport) {
        if (file == null) {
            return AjaxResult.error("文件必选");
        }

        log.info("开始导入-----");
        String filePathStr = null;
        LoginUser loginUser = SecurityUtils.getLoginUser();
        try {

            //解析文件
            ExcelUtil<BaseTeacherVo> util = new ExcelUtil<BaseTeacherVo>(BaseTeacherVo.class);
            List<BaseTeacherVo> webVOList = util.importExcel(file.getInputStream());
            int num = 1;
            for (BaseTeacherVo teacherWebWo : webVOList) {
                num++;
//                if (!IdentityUtils.isLegalIdCard(teacherWebWo.getIdCard())) {
//                    return AjaxResult.error("身份证格式不正确行数为：" + num);
//                }
            }
            for (BaseTeacherVo teacherWebWo : webVOList) {
                LambdaQueryWrapper<BaseUnit> objectLambdaQueryWrapper = new LambdaQueryWrapper<>();
                objectLambdaQueryWrapper.eq(BaseUnit::getRaceId, loginUser.getSelectRaceId());
                objectLambdaQueryWrapper.eq(BaseUnit::getUnitName, teacherWebWo.getUnitName());
                List<BaseUnit> baseUnits = unitMapper.selectList(objectLambdaQueryWrapper);
                BaseUnit unit = null;
                if (baseUnits.isEmpty()) {
                    unit = new BaseUnit();
                    unit.setRaceId(loginUser.getSelectRaceId());
                    unit.setUnitName(teacherWebWo.getUnitName());
                    unitMapper.insert(unit);
                } else {
                    unit = baseUnits.get(0);
                }
                LambdaQueryWrapper<BaseTeacher> teacherLambdaQueryWrapper = new LambdaQueryWrapper<>();
                teacherLambdaQueryWrapper.eq(BaseTeacher::getIdCard, teacherWebWo.getIdCard());
                teacherLambdaQueryWrapper.eq(BaseTeacher::getRaceId, loginUser.getSelectRaceId());
                //考生只能报一个项目
//                teacherLambdaQueryWrapper.eq(ExamStu::getSubId, subId);
                List<BaseTeacher> examStus = baseTeacherMapper.selectList(teacherLambdaQueryWrapper);
                if (examStus.isEmpty()) {
                    BaseTeacher teacher = new BaseTeacher();
                    BeanUtils.copyProperties(teacherWebWo, teacher);
                    teacher.setIdCard(teacherWebWo.getIdCard());
                    if (teacherWebWo.getSexStr() != null) {
                        teacher.setSex(teacherWebWo.getSexStr().equals("男") ? 1 : 2);
                    } else {
                        teacher.setSex(IdentityUtils.judgeSex(teacherWebWo.getIdCard()));
                    }
                    teacher.setGroupId(teacherWebWo.getGroupId());
                    teacher.setUnitId(unit.getId());
                    teacher.setRaceId(loginUser.getSelectRaceId());
                    teacher.setCreateBy(loginUser.getUser().getNickName());
                    teacher.setCreateTime(LocalDateTime.now());
                    baseTeacherMapper.insert(teacher);
                } else {
                    //更新
                    BaseTeacher teacher = examStus.get(0);
                    BeanUtils.copyProperties(teacherWebWo, teacher);
                    if (teacherWebWo.getSexStr() != null) {
                        teacher.setSex(teacherWebWo.getSexStr().equals("男") ? 1 : 2);
                    } else {
                        teacher.setSex(IdentityUtils.judgeSex(teacherWebWo.getIdCard()));
                    }
                    teacher.setGroupId(teacherWebWo.getGroupId());
                    teacher.setUnitId(unit.getId());
                    teacher.setUpdateBy(loginUser.getUser().getNickName());
                    teacher.setUpdateTime(LocalDateTime.now());
                    baseTeacherMapper.updateById(teacher);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error();
        }
        return AjaxResult.success();
    }
}
